package com.dfk.bs.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.dao.user.BaoxiuMapper;
import com.dfk.bs.entity.Room;
import com.dfk.bs.entity.User;
import com.dfk.bs.entity.vo.Baoxiu;
import com.dfk.bs.service.UserService;
import com.dfk.bs.service.user.BaoxiuService;
import com.dfk.bs.service.user.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("报修信息管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/ddkbishe/baoxiu")
public class BaoxiuController {

    @Autowired
    BaoxiuService baoxiuService;

    @Autowired
    BaoxiuMapper baoxiuMapper;

    @Autowired
    MessageService messageService;

    @Autowired
    UserService userService;


    @ApiOperation("所有报修列表")
    @GetMapping("/list")
    public R list() {
        List<Baoxiu> list = baoxiuService.list(null);
        return R.ok()
                .message("查询用电器列表成功")
                .data("items", list);
    }


    @ApiOperation("新报修")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "baoxiu", value = "报修信息", required = true)
            @RequestBody Baoxiu baoxiu) {
        baoxiuService.save(baoxiu);
        //messageService.SmsVerification(phone);

        return R.ok()
                .message("新增报修信息成功");
    }


    @ApiOperation("根据id删除报修信息")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "id", required = true)
            @PathVariable String id) {
        baoxiuService.removeById(id);

        return R.ok()
                .message("删除报修成功");
    }

    @ApiOperation("分页条件查询用报修列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        String selectname = (String) params.get("selectname");
        //业务逻辑错误，抛出自定义异常
        if (page <= 0 || limit <= 0) {
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Baoxiu> pageParam = new Page<>(page, limit);
        /*已经把查询的每一页的信息放到 pageParam中*/
        baoxiuService.pageQuery(pageParam, selectname);
        List<Baoxiu> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok()
                .message("分页条件查询报修列表成功")
                .data("total", total)
                .data("rows", records);
    }

    @ApiOperation(value = "根据ID修改用电器")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "baoxiu", value = "报修对象", required = true)
            @RequestBody Baoxiu baoxiu) {
         baoxiuService.updateById(baoxiu);
        String housecode = baoxiu.getHousecode();
        User one = userService.getOne(
                Wrappers.<User>lambdaQuery()
                        .eq(User::getHousecode, housecode), false
        );
        String telephone = one.getTelephone();

        if (baoxiu.getRemark().equals("已处理")){
             messageService.SmsVerification(telephone);
         }

        return R.ok()
                .message("修改报修成功");
    }

    @ApiOperation(value = "根据状态查询未处理的报修信息")
    @GetMapping("/selectstatic")
    public R selectByStatic() {
        List<Baoxiu> baoxiuList = baoxiuMapper.selectByStatic();

        System.out.println("baoxiulist"+baoxiuList);

        return R.ok().message("最新的四条报修信息").data("rows",baoxiuList);
    }

    @ApiOperation(value = "查询各个电器保修数量")
    @GetMapping("/eleappcount")
    public R eleappCount() {
        List<Map> maps = baoxiuMapper.eleappCount();
        return R.ok().message("查询各个电器保修数量").data("rows",maps);
    }

    @ApiOperation(value = "查询前三个月报修数量")
    @GetMapping("/threemonthcount")
    public R threeMonthCount() {
        Map map = baoxiuMapper.threeMonthCount();
        return R.ok().message("查询前三个月报修数量").data("row",map);
    }


}
